package com.beiming.sensor

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_all_sensor.*

class AllSensorsActivity : BaseActivity() {

    override fun getContentResId() = R.layout.activity_all_sensor

    override fun initView() {

    }

    lateinit var sensors: List<Sensor>

    override fun initData() {
        val sensorManager: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        val sensors = sensorManager.getSensorList(Sensor.TYPE_ALL)
        Log.d(TAG, "sensors.size:" + sensors.size)
        val adapter = SensorAdapter(sensors)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        rv_all_sensors.layoutManager = layoutManager
        rv_all_sensors.adapter = adapter

        for (sensor in sensors) {
            if (sensor.type == 33171016) {
                registerSensor(this, sensor)
            }
        }
    }

    //监听霍尔传感器
    fun registerSensor(context: Context, sensor: Sensor) {
        val sensorManager: SensorManager =
            context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
        sensorManager.registerListener(object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent?) {
                Log.d("SensorAdapter", event.toString());
            }

            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                Log.d("SensorAdapter", sensor.toString());
            }
        }, sensor, SensorManager.SENSOR_DELAY_GAME)
    }

}
